<?php

namespace Boosen\Admin\Contents\Controllers;

use Boosen\Admin\Contents\Models\Category;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Tree;

class CategoryController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(__('All categories'));

            $content->body($this->tree());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(__('Edit category'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(__('Create new category'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function tree()
    {
        return Category::tree(function (Tree $tree) {

            $tree->branch(function ($branch) {

                // $src = config('admin.upload.host') . '/' . $branch['logo'] ;

                // $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";

                return "{$branch['id']} - {$branch['name']}"; 

            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        Admin::script($this->script());
        return Category::form(function (Form $form) {

            $form->display('id', 'ID');

            $form->select('parent_id')->options(Category::selectOptions());

            $form->text('name')->rules('required');
            $form->text('slug', 'Slug')->rules('required')->attribute(['readonly' => 'readonly'])->help(__(' slug will use for uri.'));
            $form->textarea('desc')->rules('required');
            // $form->image('logo');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    private function script()
    {
        return <<<EOT
(function($) {
    $.fn.clickToggle = function(func1, func2) {
        var funcs = [func1, func2];
        this.data('toggleclicked', 0);
        this.click(function() {
            var data = $(this).data();
            var tc = data.toggleclicked;
            $.proxy(funcs[tc], this)();
            data.toggleclicked = (tc + 1) % 2;
        });
        return this;
    };
}(jQuery));

var oSlug = $('#slug').val();
var nameMonitor = function() {
    var name = $('#name').val();
    $('#slug').val(name.toLowerCase().replace(/\s+/g, '-'));
};

if (!oSlug) {
    nameMonitor();
}

$('#name').keyup(function(e) {
    nameMonitor();
});
$('#name').change(function(e) {
    nameMonitor();
});

var lock = '<i class="fa fa-lock"></i>';
var unlock = '<i class="fa fa-unlock"></i>';

$('#slug').css('background-color', 'rgb(243, 243, 243)').prev().css('cursor', 'pointer').html(lock).clickToggle(function() {
    $(this).next('#slug').css('background-color', '#ffffff').prop('readonly', false);
    $(this).html(unlock);
}, function() {
    $(this).next().css('background-color', 'rgb(243, 243, 243)').prop('readonly', true);
    $(this).html(lock);
});

EOT;
    }
}
